<?php
/*
 * @分销商城
 * @功能说明：API基础父类
 * @更新说明：暂无更新
 * @文件名 ApibaseController.class.php
 * @编码 UTF-8
 * @创建时间 2015-6-8 下午3:51:21
 * @创建人 XING
 */

namespace wap\Controller;
use Think\Controller;
use Think\Hook;
use Think\Think;
class ApibaseController extends Controller
{
	public $realSek = 0;
	/*
	 * 基类初始化
	 * @date: 2015-6-15 下午12:11:54
	 * @editor: XING
	 */
	public function __construct(){
		if(I('get.detory')){
			session_destroy();exit;
		}
		Hook::listen('action_begin',$this->config);
		//实例化视图类
		$this->view     = Think::instance('Think\View');
		//控制器初始化
		if(method_exists($this,'_initialize'))
			$this->_initialize();
		if(I('get.sek') && !(spotSek(I('get.sek')))){
			$this->assign('msg','身份信息数据错误，请重试！[1002]');
			$this->display('saler/noauth');
		}
		self::checkAuth();
	}
	
	/*
	 * 来源安全性验证
	 * @date: 2015-6-15 下午12:12:29
	 * @editor: XING
	 */
	public function checkAuth(){
		if(!session('loginUid')){
			$userUrl = urlencode('http://wap.hipaygo.cn/user/login?refUrl=http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']);
			redirect('https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx96f4e32fe927be19&redirect_uri='.$userUrl.'&response_type=code&scope=snsapi_userinfo&state=test&connect_redirect=1#wechat_redirect');
		}else{
			if(I('get.sek')){
				$userCid = spotSek(I('get.sek'));
				if(S('usek'.session('loginUid')) != I('get.sek') && $userCid != session('loginUid')){
					$setCustMsg = M('CustEntity')
								->where(array('cust_id'=>session('loginUid')))->setField(array(
										'lastest_id' => $userCid
									));
					S('usek'.session('loginUid'),I('get.sek'),7200);
				}
				$setSek = I('get.sek');
			}else{
				$setSek = mySpreadSek(M('CustEntity')->getFieldByCustId(session('loginUid'),'lastest_id'));
			}
			if(!$setSek){
				$this->assign('msg','身份信息数据错误，请重试！[1008]');
				$this->display('saler/noauth');
			}else if($setSek && $setSek != '08495d0'){
				$this->realSek = 1;
				$this->assign('sek',$setSek);
			}else{
				if(!strcmp($_SERVER['REQUEST_URI'],'/'))
					redirect('/user/index');
			}
		}
	}
	
}